package DemoText;
import java.util.HashMap;
import java.util.Map;
/**
 * 数青蛙
 */
public class LeetCode1419 {
    public int minNumberOfFrogs(String croakOfFrogs) {
        char[] nums = croakOfFrogs.toCharArray();
        String str = "croak";
        int n = str.length();
        int[] hash = new int[n];
        Map<Character, Integer> index = new HashMap<>();
        for (int i = 0; i < str.length(); i++)
            index.put(str.charAt(i), i);
        for (char ch : nums) {
            if (ch == str.charAt(0)) {
                if (hash[n - 1] != 0) hash[n - 1]--;
                hash[0]++;
            } else {
                int ret = index.get(ch);
                if (hash[ret - 1] == 0) return -1;
                else {
                    hash[ret - 1]--;
                    hash[ret]++;
                }
            }
        }
        for (int i = 0; i < n - 1; i++)
            if (hash[i] != 0) return -1;
        return hash[n - 1];
    }
}
